﻿using System.Diagnostics;
using System.IO;
using System.Text;
using UnityEngine;
using Debug = UnityEngine.Debug;

namespace XUBugly
{
    namespace Editor
    {
        internal class BuglySymbolsUpload
        {
            private static string[] soFolders = { "arm64-v8a", "armeabi-v7a" };

            private static string soCmdTemplate = "java -jar buglyqq-upload-symbol.jar -appid <APP ID> -appkey <APP KEY> -bundleid <App BundleID> -version <App Version> -platform <App Platform> -inputSymbol <Original Symbol File Path>";

            public static string GetBuglyToolPath()
            {
                return Path.GetFullPath(BuglyInitTempFolder.tempFolder);
            }

            public static string GetDebugSoPath()
            {
                return Path.GetFullPath(Application.dataPath + "/../Temp/StagingArea/symbols");
            }

            public static string GetAppId()
            {
                return XUBuglyConfig.LoadOrCreate().androidId;
            }

            public static string GetAppKey()
            {
                return XUBuglyConfig.LoadOrCreate().androidKey;
            }

            /// <summary>
            /// 打完包后调用此方法 自动上传符号表文件
            /// </summary>
            public static void UploadBuglyso()
            {
                if (!XUBuglyConfig.LoadOrCreate().uploadSymbols)
                {
                    return;
                }
                DeleteSo();
                CopyBuglySo();
                GenCommand();
                RunCommand();
            }

            private static void DeleteSo()
            {
                foreach (var folder in soFolders)
                {
                    XUBuglyUtil.DeleteFolder(GetBuglyToolPath() + "/" + folder);
                }
            }

            private static void CopyBuglySo()
            {
                XUBuglyUtil.CopyFolder(GetDebugSoPath(), GetBuglyToolPath());
            }

            private static void GenCommand()
            {
                StringBuilder sb = new StringBuilder();
                foreach (var folder in soFolders)
                {
                    string soPath = GetBuglyToolPath() + "/" + folder;
                    if (XUBuglyUtil.CheckFolder(soPath))
                    {
                        string cmd = soCmdTemplate;
                        cmd = cmd.Replace("<APP ID>", GetAppId());
                        cmd = cmd.Replace("<APP KEY>", GetAppKey());
                        cmd = cmd.Replace("<App BundleID>", Application.identifier);
                        cmd = cmd.Replace("<App Version>", Application.version);
                        cmd = cmd.Replace("<App Platform>", "Android");
                        cmd = cmd.Replace("<Original Symbol File Path>", folder);
                        sb.AppendLine(cmd);
                    }
                }
                //sb.AppendLine("pause");

                Debug.Log("command:" + sb);

                File.WriteAllText(GetBuglyToolPath() + "/upload.bat", sb.ToString());
            }

            public static void RunCommand()
            {
                ProcessStartInfo info = new ProcessStartInfo();
                info.WorkingDirectory = GetBuglyToolPath();
                info.FileName = GetBuglyToolPath() + "/upload.bat";
                info.CreateNoWindow = false;
                info.ErrorDialog = true;
                info.UseShellExecute = true;

                if (info.UseShellExecute)
                {
                    info.RedirectStandardOutput = false;
                    info.RedirectStandardError = false;
                    info.RedirectStandardInput = false;
                }
                else
                {
                    info.RedirectStandardOutput = true;
                    info.RedirectStandardError = true;
                    info.RedirectStandardInput = true;
                    info.StandardOutputEncoding = Encoding.UTF8;
                    info.StandardErrorEncoding = Encoding.UTF8;
                }

                Process process = Process.Start(info);

                if (!info.UseShellExecute)
                {
                    Debug.Log(process.StandardOutput);
                    Debug.Log(process.StandardError);
                }

                process.WaitForExit();
                process.Close();
            }
        }
    }
}